/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gameheartv2;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Image;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.border.Border;

/**
 *
 * @author GaoXteen
 */
public class GameStateFrame extends JFrame implements Runnable {

    public Vector<GameState> vGameState;
    private Thread animator;
    private boolean running;
    static public  boolean exitCurrState;
    static public boolean enterNextState;
    private long sleepTime;
    static public int currState;
    static public int priState;
    private boolean isPaused;
    public Image dbImage = null;
    JLayeredPane theLayeredPane;
    public JLabel bgLabel;
    public JPanel bgJPane;
    public Vector<Card> vButtonCard;
   
    String ip;
    public static Player player;

    //public JTextField tfChat;
    public GameStateFrame() {
       // client = new Client("localhost", 8999);
//        client.send("3");
        ip="";
        player = new Player();
        this.sleepTime = 50;
        this.running = true;
        this.enterNextState = true;
        this.exitCurrState = false;
        this.currState = GameDef.STATE_ROOM;
        vGameState = new Vector<GameState>();
        vGameState.add(new GameStateTableCard(this));
        vGameState.add(new GameStateRoom(this));
        vButtonCard = new Vector<Card>();        
       // Border border = BorderFactory.createLineBorder(Color.yellow, 5);
        theLayeredPane = getLayeredPane();
        this.setLayout(null);

        this.bgJPane = new JPanel() {

            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                if (dbImage != null) {
                    g.drawImage(dbImage, 0, 0, getWidth(), getHeight(), null);
                }
            }
        };

       // bgJPane.setBounds(5, 5, 816, 639);
        bgJPane.setBounds(0, 0, 1020, 710);
        this.getContentPane().add(this.bgJPane, "Center");
        this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

        this.setResizable(false);
    }

    public GameState GetGameState(int gsId) {
        GameState gs = null;
        for (int i = 0; i < vGameState.size(); i++) {
            gs = vGameState.get(i);
            if (gs.GAME_STATE == gsId) {
                return gs;
            }
        }
        return null;
    }

    static public void SwitchState(int stateID) {
        GameStateFrame.priState = GameStateFrame.currState;
        GameStateFrame.currState = stateID;
        GameStateFrame.exitCurrState = true;
        GameStateFrame.enterNextState = true;

    }

    public void resumeGame() // called when the JFrame is activated / deiconified
    {
        isPaused = false;
    }

    public void pauseGame() // called when the JFrame is deactivated / iconified
    {
        isPaused = true;
    }

    public void stopGame() // called when the JFrame is closing
    {
        running = false;

    }

    public void SendMessage(int message) {
        
        
            GameState gs = GetGameState(this.currState);
            if (gs != null) {
                gs.StateHandler(message);
            }
        
        
    }
    

    public void addNotify() // only start the animation once the JPanel has been added to the JFrame
    {
        super.addNotify();  
        startGame();    // start the thread
    }

    private void startGame() // initialise and start the thread
    {
        if (animator == null || !running) {
            animator = new Thread(this);
            animator.start();
        }
    }

    public void run() /* The frames of the animation are drawn inside the while loop. */ {
        while (running) {
            if (this.enterNextState) {
                this.enterNextState = false;
                this.SendMessage(GameDef.MESSAGE_ENTER);
            } else {

                this.SendMessage(GameDef.MESSAGE_UPDATE);

              //  if (!this.enterNextState) {
                    this.SendMessage(GameDef.MESSAGE_PAINT);
              //  }
            }//co sua cho nay de ne' truong hop vao thang paint khi update
//            if (this.exitCurrState) {
//                this.SendMessage(GameDef.MESSAGE_EXIT);
//                this.exitCurrState = false;
//                this.enterNextState = true;
//        }
            try {
                Thread.sleep(sleepTime);
            } catch (InterruptedException ex) {
            }

        }
        System.exit(0);   // so window disappears
    }

    @Override
    public void paint(Graphics g) {
        this.bgJPane.updateUI();
    }
}
